<?php
require_once("common.inc.php");
$id = $_GET["Id"];
$sql = "SELECT * FROM `article` WHERE Id=$id";
$conn = createDb();
$rs = $conn->query($sql);
if($conn->error) die($conn->error);
$detail = $rs->fetch_assoc();
$rs->close();

$sql = "select *  from  `reply` where ArticleId=$id";
$rs = $conn->query($sql);
$replies = [];
while($record = $rs->fetch_assoc()) $replies[] = $record;
$rs->close();

$isReplyOk=true;
$errorMessage;
if(isset($_POST["doReply"])){
    $content= $_POST["replyContent"];
    if(trim($content)===""){
        $isReplyOk = false;
        $errorMessage = "请填写内容";
    }
    $createTime = date_format(new DateTime(),'Y-m-d H:i:s');
    $authorId= $user["Id"];
    $authorName = $user["Username"];
    $articleId= $_GET["Id"];
    if($isReplyOk){
        $sql = "INSERT INTO `reply` (`Content`,`CreateTime`,`AuthorId`,`AuthorName`,`ArticleId`)
        VALUES('$content','$createTime','$authorId','$authorName','$articleId')";
        //echo $sql;
        $conn->query($sql);
        if($conn->error) die($conn->error);
        $isReplyOk = true;
    }
    
}else $isReplyOk=true;



$pageIndex = isset($_GET["pageIndex"])?$_GET["pageIndex"]:1;
$pageSize = 4;
$pageInfo = pageable("reply","ArticleId=$id","CreateTime DESC",$pageIndex,$pageSize);

$replies = $pageInfo["items"];
$pageCount =$pageInfo["pageCount"];
$recordCount = $pageInfo["total"];
function writeHtmlContent($content){
    $content = str_replace("<","&lt;",$content);
    $content = str_replace(" ","&nbsp;",$content);
    $content = str_replace("\n","<br />",$content);
    return $content;

}


?>
<?php require_once("header.inc.php");?>

<div class="dataset detail">
        <div class="field title">
            <label>
                标题
            </label>
            <span class="input"><?php echo $detail["Title"]?>"</span>
        </div>
        <div class="field content">
            <label>
                内容
            </label>
            <span class="input"><?php echo  writeHtmlContent($detail["Content"])?></span>
        </div>
        
        <div class="info">
            <?=$detail["AuthorName"]?>编辑于
            <?=$detail["UpdateTime"]?>
        </div>
        <div class="actions">
            <a href="list4.php">返回列表</a>
        </div>
        <div class="replies">
            <?php if(isset($isReplyOk) && $isReplyOk) {?>
                <div class="resultMessage">回复成功</div>
            <?php }else {?>
                <div style="color:red" class="error"><?=$errorMessage?></div>
            <?php }?>
            <form class="replyEditor" action="article_detail4.php?Id=<?=$id?>" method="post">
                <div class="caption">回复文章</div>
                <textarea name="replyContent"></textarea>
                <input type="submit" name="doReply" value="提交回复" />
            </form>

            <div class="existedRedlies">
                <?php foreach($replies as $re) {?>
                <div class="replyBox">
                    <div class="replier">
                        <?=$re["AuthorName"]?>    
                    </div>
                    <div class="createTime">
                        <?=$re["CreateTime"]?>    
                    </div>
                    <div class="replyContent">
                    <?=writeHtmlContent($re["Content"])?>
                    </div>
                </div>
                <?php }?>
                <div><?=$pageCount?>页,<?=$recordCount?>条回复
                    <?php for($i=1;$i<=$pageCount;$i++) {?>
                        <a href="article_detail4.php?pageIndex=<?=$i?>&Id=<?=$id?>"><?=$i?></a>
                    <?php }?>

                </div>
            </div>
            <form class="replyEditor" action="article_detail4.php?Id=<?=$id?>" method="post">
                <div>回复文章</div>
                <textarea name="replyContent"></textarea>
                <input type="submit" name="doReply" value="提交回复" />
            </form>
        </div>
</div>
<?php if(isset($_POST["doReply"])){?>
<script type="text/javascript">
window.onload= function scollBottom(){
    document.body.scrollTop=document.body.scrollHeight;
    }
    setTimeout(scollBottom,100);
    //window.onload= scollBottom;
    
    
</script>
<?php } ?>
<?php require_once("footer.inc.php");?>